package com.yuan.base.framework;

import com.alibaba.fastjson.JSON;
import com.yuan.base.framework.bean.UserInfo;
import com.yuan.base.utils.PreferencesUtils;


/**
 * 用户相关
 */

public class BaseUserManager {

	private static UserInfo userInfo;

	public static UserInfo getUser() {
		if (userInfo == null) {
			userInfo = JSON.parseObject(PreferencesUtils.getString(BaseAppContext.getInstance(), BaseContant.BasePreferences.USER_INFO_PRE), UserInfo.class);
		}
		return userInfo;
	}

	public static UserInfo getUserByCache() {
		return JSON.parseObject(PreferencesUtils.getString(BaseAppContext.getInstance(), BaseContant.BasePreferences.USER_INFO_PRE), UserInfo.class);
	}

	public static void setUser(UserInfo userInfo) {
		BaseUserManager.userInfo = userInfo;
	}

	public static void saveUserInfo(UserInfo userInfo) {
		PreferencesUtils.putString(BaseAppContext.getInstance(), BaseContant.BasePreferences.USER_INFO_PRE, JSON.toJSONString(userInfo));
		setUser(userInfo);
	}

	public static void saveUserInfoToPreferences(String str) {
		PreferencesUtils.putString(BaseAppContext.getInstance(), BaseContant.BasePreferences.USER_INFO_PRE, str);
		userInfo = JSON.parseObject(str, UserInfo.class);
	}

	/**
	 * 获取客户端用户的唯一标识
	 */
	public static String getUniqueCode(String str) {
		return getUser().getPhone1() + "_" + str;
	}

}
